﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace YArchitech.Plumbing
{
	public class EidtListView : ListView
	{
		public System.Drawing.Color EditColorProp
		{
			get
			{
				return this.editColor;
			}
			set
			{
				this.editColor = value;
			}
		}

		public EidtListView()
		{
			this.editColor = System.Drawing.Color.LightGreen;
			base.View = View.Details;
			base.FullRowSelect = true;
			base.TabIndex = 0;
			base.GridLines = true;
			base.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
			base.MouseDoubleClick += this.EidtListView_DoubleClick;
			base.MouseClick += this.EidtListView_Click;
			base.OwnerDraw = true;
			base.DrawItem += this.EidtListView_DrawItem;
			base.DrawSubItem += this.EidtListView_DrawSubItem;
			base.DrawColumnHeader += this.EidtListView_DrawColumnHeader;
			base.Controls.AddRange(new Control[]
			{
				this.editBox
			});
			this.editBox.KeyPress += this.editBox_KeyPress;
			this.editBox.LostFocus += this.editBox_LostFocus;
			this.editBox.BackColor = this.editColor;
			this.editBox.BorderStyle = BorderStyle.Fixed3D;
			this.HideEditBox(false);
		}

		private void editBox_LostFocus(object sender, EventArgs e)
		{
			this.HideEditBox(true);
		}

		private void editBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				this.HideEditBox(true);
			}
			if (e.KeyChar == '\u001b')
			{
				this.HideEditBox(false);
			}
			if (e.KeyChar == 'U')
			{
				this.HideEditBox(false);
			}
		}

		private void EidtListView_Click(object sender, MouseEventArgs e)
		{
			this.ProcessEditBox(e);
		}

		private void EidtListView_DoubleClick(object sender, MouseEventArgs e)
		{
			this.ProcessEditBox(e);
		}

		private void ProcessEditBox(MouseEventArgs e)
		{
			try
			{
				ListViewHitTestInfo listViewHitTestInfo = base.HitTest(e.X, e.Y);
				this.itemSelected = listViewHitTestInfo.Item;
				this.subItemSelected = listViewHitTestInfo.SubItem;
				if (this.itemSelected != null && this.subItemSelected != null && this.subItemSelected.Tag != null)
				{
					this.editBox.Left = this.subItemSelected.Bounds.Left;
					this.editBox.Top = this.subItemSelected.Bounds.Top;
					this.editBox.Width = this.subItemSelected.Bounds.Width;
					this.editBox.Height = this.subItemSelected.Bounds.Height;
					this.editBox.Show();
					this.editBox.Text = this.subItemSelected.Text;
					this.editBox.SelectAll();
					this.editBox.Focus();
				}
				else
				{
					this.itemSelected = null;
					this.subItemSelected = null;
					this.HideEditBox(true);
				}
			}
			catch
			{
			}
		}

		private void EidtListView_DrawItem(object sender, DrawListViewItemEventArgs e)
		{
			if ((e.State & ListViewItemStates.Selected) != (ListViewItemStates)0)
			{
				e.Graphics.FillRectangle(Brushes.Yellow, e.Bounds);
				e.DrawFocusRectangle();
			}
			else
			{
				e.DrawBackground();
			}
			e.DrawText();
		}

		private void EidtListView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
		{
			if (e.SubItem.Tag != null)
			{
				e.Graphics.FillRectangle(Brushes.LightGreen, e.Bounds);
				e.DrawFocusRectangle(e.Bounds);
			}
			e.DrawText();
		}

		private void EidtListView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
		{
			e.DrawBackground();
			e.DrawText(TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
		}

		private void HideEditBox(bool applyData)
		{
			if (applyData)
			{
				bool flag = false;
				try
				{
					Convert.ToDouble(this.editBox.Text);
					flag = true;
				}
				catch (Exception ex)
				{
					ex.ToString();
				}
				if (flag)
				{
					this.subItemSelected.Text = this.editBox.Text;
					if (this.notification != null && this.itemSelected.Tag != null && this.subItemSelected.Tag != null)
					{
						this.notification(this.subItemSelected.Text, (int)this.itemSelected.Tag, (int)this.subItemSelected.Tag);
					}
				}
			}
			this.editBox.Size = new Size(0, 0);
			this.editBox.Location = new System.Drawing.Point(0, 0);
			this.editBox.Text = "";
			this.editBox.Hide();
		}

		private ListViewItem itemSelected;

		private ListViewItem.ListViewSubItem subItemSelected;

		private System.Windows.Forms.TextBox editBox = new System.Windows.Forms.TextBox();

		public EidtListView.DataChangedNotification notification;

		private System.Drawing.Color editColor;

		public delegate void DataChangedNotification(string text, int itemTag, int subItemTag);
	}
}
